package mornd.test5;

import lombok.Data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.ResourcePropertySource;

import javax.swing.*;
import java.lang.reflect.Field;

/**
 * @author: mornd
 * @dateTime: 2023/5/10 - 21:06
 * 模拟 @ConfigurationProperties 注解将环境参数绑定到对象中
 */

public class App {
    public static void main(String[] args) throws Exception {
        SpringApplication spring = new SpringApplication();
        StandardEnvironment environment = new StandardEnvironment();

        environment.getPropertySources().addLast(
                new ResourcePropertySource(new ClassPathResource("step4.properties")));

        Binder binder = Binder.get(environment);

        // 第一个参数是前缀
        User user = binder.bind("user", User.class).get();
        System.out.println(user);

        User user2 = new User();
        binder.bind("user", Bindable.ofInstance(user2)).get();
        System.out.println(user2);

        binder.bind("spring.main", Bindable.ofInstance(spring));

        Field bannerMode = spring.getClass().getDeclaredField("bannerMode");
        bannerMode.setAccessible(true);
        System.out.println(bannerMode.get(spring));

        Field lazyInitialization = spring.getClass().getDeclaredField("lazyInitialization");
        lazyInitialization.setAccessible(true);
        System.out.println(lazyInitialization.get(spring));


    }

    @Data
    static class User {
        private Integer id;
        private String firstName;
        private String middleName;
        private String lastName;
    }
}
